﻿using AutoMapper;
using Azure;
using Dm;
using ERPPublic.eneity;
using MediatR;
using ProcurementManagement.Applcation.Command.VendorCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Dtos;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;

namespace ProcurementManagement.Applcation.CommandHanlers.VendorCommandHanlers
{
    public class VendorShowCommandHandlers : IRequestHandler<VendorShowCommand, PageDTO<VendorShowDtos>>
    { 
       
        protected readonly InterfaceVendorRepository repository;
        protected readonly InterfaceVendorTypeRepository Trepository;
        protected readonly IProvinceRepository Prepository;
        protected readonly ICityRepository Crepository;
        protected readonly IMapper mapper;
        ISqlSugarClient db;
        public VendorShowCommandHandlers(InterfaceVendorRepository repository, IMapper mapper, InterfaceVendorTypeRepository Trepository, IProvinceRepository Prepository, ICityRepository Crepository, ISqlSugarClient db)
        {
            this.repository = repository;
            this.mapper = mapper;
            this.Trepository = Trepository;
            this.Prepository = Prepository;
            this.Crepository = Crepository;
            this.db = db;
        }
        

        public async Task<PageDTO<VendorShowDtos>> Handle(VendorShowCommand request, CancellationToken cancellationToken)
        {
           
            // 执行连接查询并选择所需的字段  
            var query = db.Queryable<Vendor, VendorType, Province, City>((vendor, vendorType, province, city) =>
                new JoinQueryInfos(
                    JoinType.Inner, vendor.VendorTypeId == vendorType.VendorTypeId,
                    JoinType.Inner, vendor.ProvinceId == province.ProvinceId,
                    JoinType.Inner, vendor.CityId == city.CityId
                ))
                .Select((vendor, vendorType, province, city) => new VendorShowDtos
                {
                    // 映射所需字段  
                    VendorId = vendor.VendorId,
                    VNumber = vendor.VNumber,
                    VName = vendor.VName,
                    ProvinceId = vendor.ProvinceId,
                    CityId = vendor.CityId,
                    VIndustry = vendor.VIndustry,
                    BPhone = vendor.BPhone,
                    VPhone = vendor.VPhone,
                    VContact = vendor.VContact,
                    VGrade= vendor.VGrade,
                    VDepartment= vendor.VDepartment,
                    VDuty= vendor.VDuty,
                    VCorp= vendor.VCorp,
                    VFirmAddress= vendor.VFirmAddress,
                    SName= province.ProvinceName,
                    CName= city.CityName,
                    VendorTypeName = vendorType.VTName,
                    hName= vendorType.VTName,
                    AddName= vendor.AddName,
                    AddTime= vendor.AddTime,
                    UpdName= vendor.UpdName,
                    UpdTime= vendor.UpdTime,
                    VendorTypeId= vendor.VendorTypeId,
                    Office_Id= vendor.Office_Id,
                    User_Id= vendor.User_Id

                }).Where(vendor => vendor.IsDel== false);

            // 根据请求参数应用过滤  
            if (request.FId != null)
            {
                query = query.Where(vendor => vendor.VendorTypeId == request.FId);
            }
            if (request.Node != null)
            {
                query = query.Where(vendor => vendor.VNumber.Contains(request.Node));
            }
            if (request.Name != null)
            {
                query = query.Where(vendor => vendor.VName.Contains(request.Name));
            }
            if (request.Sid != null )
            {
                query = query.Where(vendor => vendor.ProvinceId == request.Sid);
            }
            if (request.Cid != null)
            {
                query = query.Where(vendor => vendor.CityId == request.Cid);
            }
            if (request.Hid != null )
            {
                query = query.Where(vendor => vendor.VIndustry == request.Hid);
            }
            if (request.BPhone != null)
            {
                query = query.Where(vendor => vendor.BPhone.Contains(request.BPhone));
            }
            if (request.SPhone != null)
            {
                query = query.Where(vendor => vendor.VPhone.Contains(request.SPhone));
            }
            if (request.ZProper != null)
            {
                query = query.Where(vendor => vendor.VContact.Contains(request.ZProper));
            }

            // 执行查询并获取总数  
            var totalCount = await query.CountAsync();

            // 应用分页  
            var vendorList = await query
                .OrderByDescending(vendor => vendor.VendorId)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToListAsync();

            // 准备结果  
            var page = new PageDTO<VendorShowDtos>
            {
                Totalcount = totalCount,
                Pagecount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize),
                list = vendorList
            };

            return page;
        }
    }
}
